package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.TravelContractDocument;
import com.ktgj.customer.service.ITravelContractDocumentService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 合同附件关联Controller
 * 
 * @author ruoyi
 * @date 2022-07-08
 */
@RestController
@RequestMapping("/document")
public class TravelContractDocumentController extends BaseController
{
    @Autowired
    private ITravelContractDocumentService travelContractDocumentService;

    /**
     * 查询合同附件关联列表
     */
//    @PreAuthorize(hasPermi = "system:document:list")
    @PostMapping("/list")
    public TableDataInfo list(TravelContractDocument travelContractDocument)
    {
        startPage();
        List<TravelContractDocument> list = travelContractDocumentService.selectTravelContractDocumentList(travelContractDocument);
        return getDataTable(list);
    }

    /**
     * 导出合同附件关联列表
     */
    @PreAuthorize(hasPermi = "system:document:export")
    @Log(title = "合同附件关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TravelContractDocument travelContractDocument) throws IOException
    {
        List<TravelContractDocument> list = travelContractDocumentService.selectTravelContractDocumentList(travelContractDocument);
        ExcelUtil<TravelContractDocument> util = new ExcelUtil<TravelContractDocument>(TravelContractDocument.class);
        util.exportExcel(response, list, "合同附件关联数据");
    }

    /**
     * 获取合同附件关联详细信息
     */
    @PreAuthorize(hasPermi = "system:document:query")
    @GetMapping(value = "/{travelContractDocumentId}")
    public AjaxResult getInfo(@PathVariable("travelContractDocumentId") Long travelContractDocumentId)
    {
        return AjaxResult.success(travelContractDocumentService.selectTravelContractDocumentById(travelContractDocumentId));
    }

    /**
     * 新增合同附件关联
     */
//    @PreAuthorize(hasPermi = "system:document:add")
//    @Log(title = "合同附件关联", businessType = BusinessType.INSERT)
    @PostMapping("/addContractDocument")
    public AjaxResult add(@RequestBody TravelContractDocument travelContractDocument)
    {
        return toAjax(travelContractDocumentService.insertTravelContractDocument(travelContractDocument));
    }

    /**
     * 修改合同附件关联
     */
    @PreAuthorize(hasPermi = "system:document:edit")
    @Log(title = "合同附件关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelContractDocument travelContractDocument)
    {
        return toAjax(travelContractDocumentService.updateTravelContractDocument(travelContractDocument));
    }

    /**
     * 删除合同附件关联
     */
//    @PreAuthorize(hasPermi = "system:document:remove")
//    @Log(title = "合同附件关联", businessType = BusinessType.DELETE)
	@PostMapping("/deleteContractDocument")
    public AjaxResult deleteContractDocument(@RequestBody Map<String,String> map)
    {
        String documentId = map.get("documentId");
        if (Strings.isNullOrEmpty(documentId))
        {
            return AjaxResult.error("合同附件ID不能为空");
        }
        int rows = travelContractDocumentService.deleteTravelContractDocumentById(Long.valueOf(documentId));
        if (rows > 0)
        {
            return AjaxResult.success("合同附件删除成功");
        } else
        {
            return AjaxResult.error("合同附件删除失败");
        }
    }
}
